package com.zrgg.futuwen.base;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;
import com.zrgg.futuwen.R;
import com.zrgg.futuwen.app.MyApp;
import com.zrgg.futuwen.utils.Logger;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportActivity;

/**
 * Created by codeest on 16/8/11.
 */

public abstract class SimpleActivity<T extends BaseContract.Presenter> extends SupportActivity {

    protected Activity mContext;
    private Unbinder mUnBinder;
    protected T presenter;
    TextView mTitle;

    public String getOut(String url) {
        return getClass().getSimpleName() + url;
    }
    public Activity getActivity() {
        return this;
    }




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        mUnBinder = ButterKnife.bind(this);
        mContext = this;
        MyApp.getInstance().addActivity(this);
        initEventAndData();
    }

//    protected void setToolBar(Toolbar toolbar, String title) {
//        toolbar.setTitle(title);
//        setSupportActionBar(toolbar);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//        getSupportActionBar().setDisplayShowHomeEnabled(true);
//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                onBackPressedSupport();
//            }
//        });
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApp.getInstance().removeActivity(this);
        mUnBinder.unbind();
        if (presenter != null) presenter.onDestroy();
    }

    protected abstract int getLayout();

    protected abstract void initEventAndData();

    protected Dialog progressDialog;

    public void showDialog() {
        showDialog("请稍等");
    }

    public void showDialog(String text) {
        if (progressDialog == null) {
            progressDialog = new Dialog(this, R.style.AlertDialogStyle);
            progressDialog.setContentView(R.layout.dialog);
            progressDialog.setCancelable(true);
            progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            CircleProgressBar progressBar = (CircleProgressBar) progressDialog.findViewById(R.id.progressBar);
            progressBar.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.text_orange);
        }
        TextView msg = (TextView) progressDialog.findViewById(R.id.id_tv_loadingmsg);
        msg.setText(text);
        progressDialog.show();
    }

    public void dismissDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        Logger.out(msg);
    }

    public void showToast(int msg) {
        String string = getResources().getString(msg);
        showToast(string);
        if (Logger.isDebug) {
            Logger.out(string);
        }
    }

    protected void setToolBar(Toolbar toolbar, String title) {
        mTitle = (TextView) findViewById(R.id.title);
        toolbar.setTitle("");
        mTitle.setText(title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressedSupport();
            }
        });
    }
}
